﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace IRemoting
{
    public class ContentPosition
    {
        public static int HEADER = 0;
        public static int USER_NAME = 1;
        public static int LOGIN_RESULT = 2;
        public static int LOGIN_REP_MSG = 3;
        public static int BROADCAST_MSG_HEADER = 4;
        public static int BROADCAST_MSG = 5;
    }

    public class BasePackage
    {
        public static int PACKAGE_LENGTH = 6;

        #region Content Property

        // Content property
        private object[] content;

        // Gets or sets content
        public object[] getContent() { return content; }
        public void setContent(object[] content) { this.content = content; }

        #endregion

        #region Constructor

        public BasePackage()
        {
            content = new object[PACKAGE_LENGTH];

            for (int i = 0; i < content.Length; i++)
            {
                content[i] = "";
            }
        }

        public BasePackage(PackageHeader header)
        {
            content = new object[PACKAGE_LENGTH];
            for (int i = 0; i < content.Length; i++)
            {
                content[i] = "";
            }

            setHeader(header);
        }

        #endregion

        #region Gets or sets header

        public PackageHeader getHeader()
        {
            string header = content[ContentPosition.HEADER].ToString();

            if (header == PackageHeader.LOGIN_REQ.ToString())
            {
                return PackageHeader.LOGIN_REQ;
            }
            else if (header == PackageHeader.SEND_BROADCAST_MESSAGE.ToString())
            {
                return PackageHeader.SEND_BROADCAST_MESSAGE;
            }
            else if (header == PackageHeader.LOGIN_REQ_REP.ToString())
            {
                return PackageHeader.LOGIN_REQ_REP;
            }
            else if (header == PackageHeader.LOGOUT_REQ.ToString())
            {
                return PackageHeader.LOGOUT_REQ;
            }
            else
            {
                return PackageHeader.NONE;
            }
        }

        public void setHeader(PackageHeader header)
        {
            content[ContentPosition.HEADER] = header.ToString();
        }

        #endregion

        #region Gets or sets user name

        public string getUsername()
        {
            return content[ContentPosition.USER_NAME].ToString();
        }

        public void setUserName(string userName)
        {
            content[ContentPosition.USER_NAME] = userName;
        }

        #endregion

        #region Gets or sets loggin result

        public bool getLogginResult()
        {
            return content[ContentPosition.LOGIN_RESULT].ToString().Equals("True");
        }

        public void setLogginResult(bool result)
        {
            content[ContentPosition.LOGIN_RESULT] = result;
        }

        #endregion

        #region Gets or sets loggin request message

        public string getLogginReplyMessage()
        {
            return content[ContentPosition.LOGIN_REP_MSG].ToString();
        }

        public void setLogginReplyMessage(string message)
        {
            content[ContentPosition.LOGIN_REP_MSG] = message;
        }

        #endregion

        #region Gets or sets broadcast massage header

        public BroadcastMassgeHeader getBroadcastMassageHeader()
        {
            string broadcastHeader = content[ContentPosition.BROADCAST_MSG_HEADER].ToString();

            if (broadcastHeader.Equals(BroadcastMassgeHeader.MODIFIED_ORDER.ToString()))
            {
                return BroadcastMassgeHeader.MODIFIED_ORDER;
            }
            else if (broadcastHeader.Equals(BroadcastMassgeHeader.NEW_ORDER.ToString()))
            {
                return BroadcastMassgeHeader.NEW_ORDER;
            }
            else if (broadcastHeader.Equals(BroadcastMassgeHeader.READY_TO_SERVE_ORDER.ToString()))
            {
                return BroadcastMassgeHeader.READY_TO_SERVE_ORDER;
            }
            else
            {
                return BroadcastMassgeHeader.NONE;
            }
        }

        public void setBroadcastMassageHeader(BroadcastMassgeHeader header)
        {
            content[ContentPosition.BROADCAST_MSG_HEADER] = header.ToString();
        }

        #endregion

        #region Gets or sets broadcast massage

        public string getBroadcastMassage()
        {
            return content[ContentPosition.BROADCAST_MSG].ToString();
        }

        public void setBroadcastMassage(string message)
        {
            content[ContentPosition.BROADCAST_MSG] = message;
        }

        #endregion

        #region Converter

        // Convert content to string
        public string convertToString()
        {
            string result = "";

            for (int i = 0; i < content.Length; i++)
            {
                if (i < content.Length - 1)
                {
                    result += content[i].ToString() + ",";
                }
                else if (i == content.Length - 1)
                {
                    result += content[i].ToString();
                }
            }

            return result;
        }

        // Convert content from string
        public void convertFromString(string content)
        {
            string[] fragments = content.Split(',');

            if (fragments.Length > 0)
            {
                for (int i = 0; i < fragments.Length; i++)
                {
                    this.content[i] = fragments[i];
                }
            }
        }

        #endregion
    }
}
